package com.czy.owner.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.WheelAttrParentModel;
import com.czy.owner.widget.MyRecyclerView;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;


/**
 * Time:1/12/18 21:50
 * Created by Curtain.
 */

public class WheelOtherAttrAdapter extends RecyclerArrayAdapter<WheelAttrParentModel> {

    private Context context;

    public WheelOtherAttrAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new  WheelOtherAttrHolder(parent);
    }


    class  WheelOtherAttrHolder extends BaseViewHolder<WheelAttrParentModel> {

        private TextView item_tv_attr_name;
        private TextView item_tv_all;
        private MyRecyclerView recyclerViewItem;
        private WheelBrandAdapter mAdapter;

        public WheelOtherAttrHolder(ViewGroup parent) {
            super(parent, R.layout.item_wheel_other_attr_layout);
            item_tv_attr_name = $(R.id.item_tv_attr_name);
            item_tv_all = $(R.id.item_tv_all);
            recyclerViewItem = $(R.id.recycler_view_item);
        }

        @Override
        public void setData(final WheelAttrParentModel model) {
            super.setData(model);

            recyclerViewItem.setLayoutManager(new GridLayoutManager(context,3));
            mAdapter = new WheelBrandAdapter(context);
            recyclerViewItem.setAdapter(mAdapter);



            //栅格布局监听
            mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position, Object o) {
                    Log.d("RAVEN","OnChildItemClick==adapterPostion" +getAdapterPosition());
                    Log.d("RAVEN","OnChildItemClick==position" +position);
                    mAdapter.getAllData().get(position).setChoosed(!mAdapter.getAllData().get(position).isChoosed());
                }
            });

            item_tv_attr_name.setText(model.getName());
            if(model.getAttrModelList().size() <= 9){
                item_tv_all.setVisibility(View.GONE);
            }else {
                item_tv_all.setVisibility(View.VISIBLE);
            }

            //Adpater 只添加9项
            for(int i = 0 ; i < (model.getAttrModelList().size() > 9 ? 9 : model.getAttrModelList().size());++i){
                mAdapter.add(model.getAttrModelList().get(i));
            }

            item_tv_all.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemAllClickedListener != null){
                        onItemAllClickedListener.onItemAllClicked(getAdapterPosition());
                    }

                }
            });
        }
    }

    private OnItemAllClickedListener onItemAllClickedListener;
    public interface OnItemAllClickedListener{
        void onItemAllClicked(int parentPosition);
    }

    public void setOnItemAllClickedListener(OnItemAllClickedListener listener){
        this.onItemAllClickedListener = listener;
    }


}
